// ================================
// ee_hqxaa  Hilda,
// this is a program to test our SD driver
// version 1.0  @ 24 July
//=================================


#define F_CPU 7372800UL


#include<avr/io.h>
#include<util/delay.h>
#include"SD.h"
#include"FAT.h"

#define PATH (unsigned char *) ("\\")


#define DEBUGLED1_CON() DDRA|=_BV(PA0)
#define DEBUGLED1_ON()  PORTA|=_BV(PA0)
#define DEBUGLED1_OFF() PORTA&=~_BV(PA0)

#define DEBUGLED2_CON() DDRA|=_BV(PA1)
#define DEBUGLED2_ON()  PORTA|=_BV(PA1)
#define DEBUGLED2_OFF() PORTA&=~_BV(PA1)

#define DEBUGLED3_CON() DDRA|=_BV(PA2)
#define DEBUGLED3_ON()  PORTA|=_BV(PA2)
#define DEBUGLED3_OFF() PORTA&=~_BV(PA2)

#define DEBUGLED4_CON() DDRA|=_BV(PA3)
#define DEBUGLED4_ON()  PORTA|=_BV(PA3)
#define DEBUGLED4_OFF() PORTA&=~_BV(PA3)

#define DEBUGLED5_CON() DDRA|=_BV(PA4)
#define DEBUGLED5_ON()  PORTA|=_BV(PA4)
#define DEBUGLED5_OFF() PORTA&=~_BV(PA4)

#define DEBUGLED6_CON() DDRA|=_BV(PA5)
#define DEBUGLED6_ON()  PORTA|=_BV(PA5)
#define DEBUGLED6_OFF() PORTA&=~_BV(PA5)

#define DEBUGLED7_CON() DDRA|=_BV(PA6)
#define DEBUGLED7_ON()  PORTA|=_BV(PA6)
#define DEBUGLED7_OFF() PORTA&=~_BV(PA6)

#define DEBUGLED8_CON() DDRA|=_BV(PA7)
#define DEBUGLED8_ON()  PORTA|=_BV(PA7)
#define DEBUGLED8_OFF() PORTA&=~_BV(PA7)




void delay500ms(){
	unsigned char cnt;
	for(cnt = 0; cnt< 30; cnt++){
		_delay_ms(20);
	}
}




int main(void){

	DDRA = 0xff;

	delay500ms();

	DEBUGLED1_CON();					// initialize debug led
	DEBUGLED1_OFF();
	DEBUGLED2_CON();
	DEBUGLED2_OFF();
	DEBUGLED3_CON();					// initialize debug led
	DEBUGLED3_OFF();
	DEBUGLED4_CON();
	DEBUGLED4_OFF();
	DEBUGLED5_CON();					// initialize debug led
	DEBUGLED5_OFF();
	DEBUGLED6_CON();
	DEBUGLED6_OFF();
	DEBUGLED7_CON();					// initialize debug led
	DEBUGLED7_OFF();
	DEBUGLED8_CON();
	DEBUGLED8_OFF();



	SD_Init();

	DEBUGLED1_ON();

	unsigned char SD_try = 0x00;		// since SD is insert by user, we need tocheck it more patiently

	while ( SD_Reset()){				// SD initialize, if failed, light up debug2 and stop
		SD_try++;
		if ( SD_try > 30 )
		while(1){
			DEBUGLED2_ON();
			delay500ms();
			DEBUGLED2_OFF();
			delay500ms();
		}
	}
	DEBUGLED2_ON();
	delay500ms();
	DEBUGLED2_OFF();
	delay500ms();


	//=================================================
	//==================test FAT system================
	//=================================================
	unsigned char buf[512];


	if(SD_ReadSingleBlock(1000,buf))while(1){
		DEBUGLED2_ON();
		delay500ms();
		DEBUGLED2_OFF();
		delay500ms();
	}

	DEBUGLED3_ON();
	delay500ms();
	DEBUGLED3_OFF();
	delay500ms();



	if(FAT_Init())while(1){
		DEBUGLED2_ON();
		delay500ms();
		DEBUGLED2_OFF();
		delay500ms();
	}

	DEBUGLED4_ON();
	delay500ms();
	DEBUGLED4_OFF();
	delay500ms();



	unsigned long cluster = 7;
	unsigned char part = 7;


	if( FAT_LoadPartCluster( cluster, part, buf) ){
		while(1){
		DEBUGLED2_ON();
		delay500ms();
		DEBUGLED2_OFF();
		delay500ms();
		}
	}

	DEBUGLED5_ON();
	delay500ms();
	DEBUGLED5_OFF();
	delay500ms();


	unsigned long Clus;
	Clus =  FAT_OpenDir(PATH);
	if(Clus == 1){
		DEBUGLED2_ON();
		delay500ms();
		DEBUGLED2_OFF();
		delay500ms();
	}

	DEBUGLED6_ON();
	delay500ms();
	DEBUGLED6_OFF();
	delay500ms();

/*
	unsigned char onesong;
	struct direntry MusicInfo;
	unsigned char count;
	count = 1;
	unsigned char type;
	type = 1;
	onesong =  search(PATH, &MusicInfo, &count, &type);

	if(onesong == 0){
		DEBUGLED7_ON();
		delay500ms();
		DEBUGLED7_OFF();
		delay500ms();
	}else{
		while(1){
			DEBUGLED2_ON();
			delay500ms();
			DEBUGLED2_OFF();
			delay500ms();
		}
	}


*/


	return 0;
}


